package com.anlu.concurrent.ch3;

import java.util.concurrent.atomic.AtomicReference;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * Copyright: 天津联想协同科技有限公司
 * <p>
 *
 * @author anlu
 * @date 2021/9/7 15:51
 */
public class UseAtomicReference {
    static AtomicReference<UserInfo> userRef = new AtomicReference<>();

    public static void main(String[] args) {
        //要修改的实体的实例
        UserInfo user = new UserInfo("Mark", 15);
        userRef.set(user);
        //要变化的新实例
        UserInfo updateUser = new UserInfo("Bill",17);
        userRef.compareAndSet(user,updateUser);

        System.out.println(userRef.get().getName());
        System.out.println(userRef.get().getAge());
        System.out.println(user.getName());
        System.out.println(user.getAge());
    }


    //定义一个实体类
    static class UserInfo {
        private String name;
        private int age;
        public UserInfo(String name, int age) {
            this.name = name;
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public int getAge() {
            return age;
        }
    }
}
